// components/bind-phone/index.js
import api from "../../../../utils/api"
import CONFIG from '../../../../utils/config'

const app = getApp()

Component({
    properties: {
        msgContent: {
            type: String,
        },
        show1: {
            type: Boolean,
            value: true
        },
    },
    options: {
        addGlobalClass: true
    },
    data: {
        btn: {
            text: '发送验证码',
            time: 60,
            timeFn: null,
            disabled: false
        },
        form: {
            phone: '',
            code: ''
        },
        show: true
    },
    methods: {
        // 获取验证码
        getYzmFn() {
            let btn = this.data.btn
            const sTime = btn.time
            btn.time -= 1
            this.setData({
                'btn.disabled': true,
                'btn.time': btn.time,
                'btn.text': `${btn.time}S后重新发送`
            })
            btn.text = `${btn.time}S后重新发送`
            btn.timeFn = setInterval(() => {
                btn.time -= 1
                this.setData({
                    'btn.disabled': true,
                    'btn.time': btn.time,
                })
                if (btn.time <= 0) {
                    clearInterval(btn.timeFn)
                    this.setData({
                        'btn.disabled': false,
                        'btn.text': '发送验证码',
                        'btn.time': sTime
                    })
                    return false
                }
                this.setData({
                    'btn.text': `${btn.time}S后重新发送`
                })
            }, 1000)
        },
        // 输入框双向绑定
        handleModelValue(e) {
            let value = e.detail.value
            let model = e.target.dataset.model
            this.setData({
                [model]: value
            })
        },
        // 提交绑定
        submit() {
            let form = this.data.form
            if (form.phone.length < 11) {
                wx.showToast({
                    icon: 'none',
                    title: '请输入正确的手机号码',
                })
                return
            }
            if (form.code.length === 0) {
                wx.showToast({
                    icon: 'none',
                    title: '请输入验证码',
                })
                return
            }

            api.post('/api/user/savemobile', {
                data: {
                    code: form.code,
                    mobile: form.phone
                }
            }).then(res => {
                app.mdFn({
                    eventId: '8120',
                    eventDesc: '绑定'
                })
                wx.showToast({
                    title: '绑定成功',
                })
                wx.setStorageSync('mobile', this.data.form.phone)
                this.triggerEvent('success')
            }).catch(e => {
                console.log(e)
                this.triggerEvent('error')
                app.mdFn({
                    eventId: '8120',
                    eventDesc: '绑定'
                })
                wx.showToast({
                    icon: 'none',
                    title: '绑定失败：' + e.faultstring,
                })
            })
        },
        // 获取验证码
        getYzm() {
            let phone = this.data.form.phone
            if (phone.length < 11) {
                wx.showToast({
                    icon: 'none',
                    title: '请输入正确的手机号码',
                })
                return
            }

            api.post('/api/login/sendverifcode', {
                data: {
                    account: phone
                }
            }).then(res => {
                wx.showToast({
                    title: '发送成功',
                })
                this.getYzmFn()
                console.log(res)

            }).catch(e => {
                wx.showToast({
                    icon: 'none',
                    title: '发送失败',
                })
            })
        },
        handlerPhoneNumber(res) {
            console.log(res)
            if (res.detail.errMsg === 'getPhoneNumber:ok') {
                // 获取成功，开始绑定
                app.isLogin({ctx:this}).then(() => {
                    this.saveWxMobile(res)
                }).catch(e => {
                    this.saveWxMobile(res)
                })
            }
        },
        saveWxMobile(res){
            api.post(`/api/user/savewamobile/${CONFIG.orgCode}/${CONFIG.appCode}`, {
                data: {
                    encrypteddata: res.detail.encryptedData,
                    iv: res.detail.iv,
                }
            }).then(res => {
                console.log(res)
                wx.showToast({
                    title: '绑定成功',
                })
                app.isLogin({ctx:this})
                this.triggerEvent('success')
            }).catch(e => {
                console.log(e)
                wx.showToast({
                    icon: 'none',
                    title: '绑定失败：' + e.faultstring,
                })
                this.triggerEvent('error')
            })
        },
        handlerShowForm() {
            this.setData({
                show: !this.data.show
            })
        }
    }
})
